package org.csu.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.bbs.entity.Notice;
import org.csu.bbs.entity.Posts;
import org.csu.bbs.persistence.NoticeMapper;
import org.csu.bbs.result.Result;
import org.csu.bbs.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public List<Notice> GetAll() {

        return noticeMapper.selectList(null);
    }

    @Override
    public List<Notice> GetBySection(Integer sectionid) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sectionid",sectionid);
        return noticeMapper.selectList(queryWrapper);
    }
    @Override
    public List<Notice> GetById(Integer id) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return noticeMapper.selectList(queryWrapper);
    }

    @Override
    public int DeleteById(Integer id) {
        return noticeMapper.delete(new QueryWrapper<Notice>().eq("id",id));
    }

    @Override
    public Result AddNotice(Notice notice) {
        try {
            noticeMapper.insert(notice);
            return new Result(200);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400);
        }
    }

    @Override
    public Notice UpdateNotice(Notice notice) {
        try {
            noticeMapper.updateById(notice);
            return null;
        }
        catch (Exception e){
            e.printStackTrace();
            return notice;
        }
    }
    @Override
    public  List<Notice> search( String keywords ){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(keywords!= null, "name", keywords).or().like(keywords!=null,"content",keywords);
        return noticeMapper.selectList(queryWrapper);
    }
    @Override
    public void changeContent( Notice notice){
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();

       updateWrapper.eq("id",notice.getId()).set("content", notice.getContent());
       noticeMapper.update(null,updateWrapper);

    }

}
